package priv.zgh.forum.game.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import priv.zgh.forum.game.entity.Type;
import priv.zgh.forum.game.mapper.TypeMapper;
import priv.zgh.forum.game.service.TypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import priv.zgh.forum.game.vo.OneGameType;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ZGH
 * @since 2022-04-19
 */
@Service
public class TypeServiceImpl extends ServiceImpl<TypeMapper, Type> implements TypeService {

    @Override
    public List<OneGameType> getAllGameType() {
        //查询所有一级分类
        QueryWrapper<Type> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("parent_id","0");
        List<Type> parentTypes = baseMapper.selectList(wrapperOne);
        //查询所有子集分类
        QueryWrapper<Type> wrapperTow = new QueryWrapper<>();
        wrapperTow.ne("parent_id","0");
        List<Type> allTwoGameType = baseMapper.selectList(wrapperTow);
        //封装最终数据
        List<OneGameType> allGameType = new ArrayList<>();
        for(Type oneItem:parentTypes){
            OneGameType oneGameType = new OneGameType();
            oneGameType.setId(oneItem.getId());
            oneGameType.setTitle(oneItem.getTitle());
            for(Type twoItem:allTwoGameType){
                if(twoItem.getParentId().equals(oneItem.getId())){
                    oneGameType.getChildren().add(twoItem);
                }
            }
            allGameType.add(oneGameType);
        }
        return allGameType;
    }
}
